package com.soft.collateral.mapper;

import com.soft.collateral.dto.MenuDTO;
import com.soft.collateral.entity.Menu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.soft.collateral.entity.Meta;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;

public interface MenuMapper extends BaseMapper<Menu> {

    //根据用户ID查询一级菜单
    @Select("select m.* \n" +
            "from users u join user_role ur join roles r join role_menu rm join menu m\n" +
            "on u.id = ur.user_id and ur.role_id = r.id and r.id = rm.role_id and rm.menu_id = m.id\n" +
            "where u.id = #{uid} and m.level=1 \n" +
            "order by m.priority")
    @Results({
            @Result(column = "meta_id" , property = "meta" , javaType = Meta.class ,
                    one = @One(select = "com.soft.collateral.mapper.MetaMapper.selectById"))
    })
    public List<Menu> selectFirstMenuByUid(Long uid);


    //根据父项ID，查询对应的二级菜单
    @Select("select m2.*\n" +
            "from menu m1 join menu m2\n" +
            "on m1.id = m2.parentMenuId\n" +
            "where m1.id = #{parentId}\n" +
            "order by m2.priority")
    @Results({
            @Result(column = "meta_id" , property = "meta" , javaType = Meta.class ,
                    one = @One(select = "com.soft.collateral.mapper.MetaMapper.selectById"))
    })
    public List<MenuDTO> selectSecondMenuByParentId(Integer parentId);

}